home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 13 - 1997 (partial) / 13.02 Feb 97 / SerialKiller SN Generator / Generator.c next >
Encoding:
C/C++ Source or Header  |  1996-10-22  |  7.6 KB  |  311 lines  |  [TEXT/CWIE]

  1. //////////////////////////////////////////////////
  2. //                                                //
  3. //    Generator.c                                    //
  4. //                                                //
  5. //    Copyright 1996 1 A.M. Productions            //
  6. //    Aug 8,1996                                    //
  7. //                                                //
  8. //    This demonstrates serial encoding based     //
  9. //    on simple mathmatical functions performed     //
  10. //    on one seed number split in half. Both         //
  11. //    encoding processes are the same for the        //
  12. //    sake of demonstration.                        //
  13. //                                                //
  14. //    Output is saved as a tab delimited Excel    //
  15. //    document.                                    //
  16. //////////////////////////////////////////////////
  17.  
  18. #ifndef __MAIN__
  19. #include "Generator.h"
  20. #endif
  21.  
  22. void main()
  23. {
  24.     long    start, end;
  25.  
  26.     InitGraf(&qd.thePort);
  27.     InitFonts();
  28.     FlushEvents(everyEvent,0);
  29.     InitWindows();
  30.     InitMenus();
  31.     TEInit();
  32.     InitDialogs(0L);        
  33.         
  34.     GetNumbers(&start,&end);        // Go ask the user for the range of numbers
  35.     CalculateValues(start,end);        // Save them out to a file
  36.  
  37.     FlushEvents(everyEvent,0);        
  38. }
  39.  
  40. void CalculateValues(long start, long end)
  41. {
  42.     long                lp, inOutCount;
  43.     short                fRefNum;
  44.     StandardFileReply    fReply;
  45.     Ptr                    bufPtr;
  46.     Str255                converted;
  47.     Str255                header = "\pCustomer #\tVersion\tSerial Number\n";
  48.     Str255                cusNum = "\p000000   ";
  49.     OSErr                iErr;
  50.     
  51.         
  52.     StandardPutFile("\pSave Serial Numbers","\pSerial Numbers",&fReply); // Get a filename from user
  53.  
  54.     if (fReply.sfReplacing)                                // If replacing old file then delete
  55.         iErr = FSpDelete(&fReply.sfFile);
  56.  
  57.     if (fReply.sfGood)
  58.     {
  59.         FSpCreate(&(fReply).sfFile,'XCEL','TEXT',fReply.sfScript);            // Make the file
  60.         iErr = FSpOpenDF(&(fReply).sfFile,fsRdWrPerm,&fRefNum);                // Open this file
  61.         if (iErr)                                        // Any problems quit
  62.             ExitToShell();
  63.     }
  64.  
  65.     inOutCount = header[0];                                // Set amount to length of string
  66.     bufPtr = (char *)&header[1];                        // Point to the first character in string
  67.     iErr = FSWrite(fRefNum,&inOutCount,bufPtr);            // Write header string to file
  68.     
  69.     for (lp=start; lp<=end; lp++)
  70.     {
  71.         LongToStr(lp,cusNum);                            // Convert number to string
  72.         inOutCount = 9;                                    // Number of digits in customer number + tab + space + tab
  73.         cusNum[7] = 0x09;                                // Force a tab into the string
  74.         cusNum[9] = 0x09;                                // Force another tab into the string
  75.         bufPtr = (char *)&cusNum[1];                    // Point to the first character in string
  76.         iErr = FSWrite(fRefNum,&inOutCount,bufPtr);        // Write customer number, tab, version, tab
  77.         if (iErr)                                        // Any problems - exit
  78.             ExitToShell();
  79.         ProduceValue(lp, converted);                    // Convert number to code
  80.         inOutCount = 16;                                // Number of digits in serial number plus three dashes + return
  81.         converted[16]=0x0d;
  82.         bufPtr = (char *)&converted[1];                    // Point to the first character in string
  83.         iErr = FSWrite(fRefNum,&inOutCount,bufPtr);        // Write serial number
  84.         if (iErr)
  85.         {
  86.             iErr = FSClose(fRefNum);
  87.             ExitToShell();
  88.         }
  89.         if (ThisKey(kESC))                                // Just in case this is taking way
  90.         {                                                // too long! Abort key.
  91.             iErr = FSClose(fRefNum);
  92.             ExitToShell();
  93.         }
  94.     }
  95.         
  96.     iErr = FSClose(fRefNum);                            // Close up the file
  97.     if (iErr)                                            // Any problems - quit
  98.         ExitToShell();
  99.         
  100. }
  101.  
  102. void ProduceValue(long num, Str255 retStr)
  103. {
  104.     long    i,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12;
  105.     long    val1,val2,val3,val4;
  106.     long    temp1, temp2;
  107.     Str255    valStr;
  108.     
  109.     i=1;
  110.     
  111.     LongToStr(num,valStr);
  112.     
  113.     d1 = valStr[i++] - '0';
  114.     d2 = valStr[i++] - '0';
  115.     d3 = valStr[i++] - '0';
  116.     d10 = valStr[i++] - '0';
  117.     d11 = valStr[i++] - '0';
  118.     d12 = valStr[i++] - '0';
  119.     
  120.     d1 = (d1*100);
  121.     d2 = (d2*10);
  122.     
  123.     temp1 = d1+d2+d3;
  124.     
  125.     d2 = d2 + 10;
  126.     d3 = d3 + 3;
  127.     d2 = d2 + ((d3/10)*10);
  128.     d1 = d1 + ((d2/100)*100);
  129.     d1 = d1-((d1/1000)*1000);
  130.     d2 = d2-((d2/100)*100);
  131.     d3 = d3-((d3/10)*10);             // Add 13
  132.     
  133.     val1 = (d3*100)+(d2)+(d1/100);    // Stir Numbers
  134.     val1 = val1 * 3;                // Multiply by 3
  135.     val1 = val1 - (temp1*2);        // Subtract the original * 2
  136.     if (val1<0)
  137.         val1 = -val1;                // Adjust if necessary
  138.     d3 = (val1 % 10);                 // Reassign values
  139.     d2 = ((val1 % 100)-d3)/10;
  140.     d1 = ((val1 % 1000)-((d2*10)+d3))/100;
  141.     
  142.     val1 = (d1*100)+(d2*10)+d3;
  143.     
  144.     // Second Number Set
  145.     
  146.     d10 = (d10*100);
  147.     d11 = (d11*10);
  148.     
  149.     temp2 = d10+d11+d12;
  150.     
  151.     d11 = d11 + 10;
  152.     d12 = d12 + 3;
  153.     d11 = d11 + ((d12/10)*10);
  154.     d10 = d10 + ((d11/100)*100);
  155.     d10 = d10-((d10/1000)*1000);
  156.     d11 = d11-((d11/100)*100);
  157.     d12 = d12-((d12/10)*10);            // Add 13
  158.     
  159.     val4 = (d12*100)+(d11)+(d10/100);    // Stir Numbers
  160.     val4 = val4 * 3;                    // Multiply by 3
  161.     val4 = val4 - (temp2*2);            // Subtract the original * 2
  162.     if (val4<0)
  163.         val4 = -val4;                    // Adjust if necessary
  164.     d12 = (val4 % 10);                    // Reassign values
  165.     d11 = ((val4 % 100)-d12)/10;
  166.     d10 = ((val4 % 1000)-((d11*10)+d12))/100;
  167.     
  168.     val4 = (d10*100)+(d11*10)+d12;
  169.     
  170.     retStr[0] = 15;                        // Set string length to digits + 3 dashes
  171.     retStr[1] = valStr[1];
  172.     retStr[2] = valStr[2];
  173.     retStr[3] = valStr[3];
  174.     retStr[4] = '-';
  175.     if (val1<1000)
  176.         retStr[5] = (((val1 % 1000)-((d2*10)+d3))/100)+'0';
  177.     else
  178.         retStr[5] = '9';
  179.     retStr[6] = (((val1 % 100)-d3)/10)+'0';
  180.     retStr[7] = (val1 % 10)+'0';
  181.     retStr[8] = '-';
  182.     if (val4<1000)
  183.         retStr[9] = (((val4 % 1000)-((d11*10)+d12))/100)+'0';
  184.     else
  185.         retStr[9] = '9';
  186.     retStr[10] = ((val4 % 100)-d12)/10+'0';
  187.     retStr[11] = (val4 % 10)+'0';
  188.     retStr[12] = '-';
  189.     retStr[13] = valStr[4];
  190.     retStr[14] = valStr[5];
  191.     retStr[15] = valStr[6];
  192.     
  193. }
  194.  
  195. void GetNumbers(long *start,long *end)
  196. {
  197.     short         itemHit, itemType;
  198.     Handle         itemHandle;
  199.     Rect         itemRect;
  200.     Str255        number;    
  201.     DialogPtr     dialog;
  202.     GrafPtr     oldPort;
  203.  
  204.     GetPort(&oldPort);
  205.     dialog=GetNewDialog(128,nil,(WindowPtr)-1);
  206.     ShowWindow(dialog);
  207.     SelectWindow(dialog);
  208.     SetPort(dialog);
  209.     GetDialogItem(dialog,3,&itemType,&itemHandle,&itemRect);
  210.     SetDialogItemText(itemHandle,"\p1");
  211.     GetDialogItem(dialog,4,&itemType,&itemHandle,&itemRect);
  212.     SetDialogItemText(itemHandle,"\p100");
  213.     
  214.     itemHit=-1;
  215.  
  216.     while ((itemHit !=1)  && (itemHit !=2))
  217.         ModalDialog(StdFilter, &itemHit);
  218.         
  219.     if (itemHit == 2)
  220.     {
  221.         SetPort(oldPort);
  222.         DisposeDialog(dialog);
  223.         ExitToShell();
  224.     }
  225.     if (itemHit == 1)
  226.     {
  227.         GetDialogItem(dialog,3,&itemType,&itemHandle,&itemRect);
  228.         GetDialogItemText(itemHandle,number);
  229.         StringToNum(number,start);
  230.         
  231.         GetDialogItem(dialog,4,&itemType,&itemHandle,&itemRect);
  232.         GetDialogItemText(itemHandle,number);
  233.         StringToNum(number,end);
  234.         
  235.         if ((*end)<(*start))
  236.         {    
  237.             SetPort(oldPort);
  238.             *start = *end;
  239.             *end = (*end) + 100;
  240.         }
  241.         if ((*end) > 999999)
  242.             (*end) = (*start) + 10;        
  243.     }
  244.     SetPort(oldPort);
  245.     DisposeDialog(dialog);
  246. }
  247.  
  248. pascal Boolean StdFilter(DialogPtr theDialog, EventRecord *theEvent, short *itemHit)
  249. {
  250.     char theChar;
  251.     short itemKind;
  252.     Handle itemHandle;
  253.     Rect itemBox;
  254.  
  255.     switch ( theEvent->what )
  256.     {
  257.     case keyDown: 
  258.         theChar = (char)(theEvent->message & charCodeMask);
  259.         if ( (((theEvent->modifiers & cmdKey) != 0) && (theChar == '.')) || (theChar == (char)27) ) /*cmd-. or ESC*/
  260.         {
  261.             *itemHit = kCancelButton;
  262.             GetDItem(theDialog, kCancelButton, &itemKind, &itemHandle, &itemBox);
  263.             HiliteControl((ControlHandle)itemHandle, 1);
  264.             return true;
  265.         }
  266.         if ( (theChar == (char)13) || (theChar == (char)3) )
  267.         {
  268.             *itemHit = kOKButton;
  269.             GetDItem(theDialog, 1, &itemKind, &itemHandle, &itemBox);
  270.             HiliteControl((ControlHandle)itemHandle, kOKButton);
  271.             return true;
  272.         }
  273.         break;
  274.     case updateEvt:
  275.         BeginUpdate(theDialog);
  276.         SetPort(theDialog);
  277.         DrawDialog(theDialog);
  278.         GetDItem(theDialog, kOKButton, &itemKind, &itemHandle, &itemBox);
  279.         InsetRect(&itemBox, -4, -4);
  280.         PenSize(3, 3);
  281.         FrameRoundRect(&itemBox, 15, 15);
  282.         EndUpdate(theDialog);
  283.         break;
  284.     }
  285.     return false;
  286. }
  287.  
  288. void LongToStr(long num, Str255 str)
  289. {
  290.     long    d, counter=6;
  291.         
  292.     if (num)
  293.     {
  294.         d = num;    
  295.         for (d=1; d<7; d++)
  296.             str[d] = '0';                
  297.         str[counter+1] = '\0';
  298.         str[0] = counter;
  299.         while (num > 0)
  300.         {
  301.             d = num/10;
  302.             str[counter--] = num - (d*10) + '0';
  303.             num = d;
  304.         }
  305.     } else
  306.     {
  307.         str[0] = 1;
  308.         str[1] = '0';
  309.         str[2] = '\0';
  310.     }
  311. }